package org.ptkparty.client.view.championships;

import org.ptkparty.client.activity.ChampionshipsActivity;
import org.ptkparty.client.model.ChampionshipProxy;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.IsEditor;
import com.google.gwt.editor.client.adapters.EditorSource;
import com.google.gwt.editor.client.adapters.ListEditor;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

public class ChampionshipsView extends Composite implements IsEditor<ListEditor<ChampionshipProxy, ChampionshipView>> {

	private static ChampionshipsViewUiBinder uiBinder = GWT.create(ChampionshipsViewUiBinder.class);

	interface ChampionshipsViewUiBinder extends UiBinder<Widget, ChampionshipsView> {
	}

	private ChampionshipsActivity activity;
	private ListDataProvider<ChampionshipProxy> provider;
	private SingleSelectionModel<ChampionshipProxy> selectionModel;
	private ListEditor<ChampionshipProxy, ChampionshipView> editor;

	@UiField(provided = true)
	CellList<ChampionshipProxy> championchipList;

	@UiField
	SimplePanel championshipPanel;

	@UiField
	Button addBtn;

	@UiHandler("addBtn")
	void onAddChampionshipBtnClicked(ClickEvent event) {
		ChampionshipProxy newChampionship = activity.addChampionship();
		editor.getList().add(newChampionship);
		provider.refresh();
		selectionModel.setSelected(newChampionship, true);
	}

	@UiField
	Button deleteBtn;

	@UiHandler("deleteBtn")
	public void onDeleteChampionshipClicked(ClickEvent clickEvent) {
		activity.delete(selectionModel.getSelectedObject());
	}

	@UiField
	Button saveBtn;

	@UiHandler("saveBtn")
	public void onSaveBtnClicked(ClickEvent clickEvent) {
		activity.save();
	}

	@UiField
	Button regenerateBtn;

	@UiHandler("regenerateBtn")
	public void onRegenerateBtnClicked(ClickEvent clickEvent) {
		activity.regenerate();
	}

	public ChampionshipsView() {
		championchipList = new CellList<ChampionshipProxy>(new ChampionshipCell());

		provider = new ListDataProvider<ChampionshipProxy>();
		provider.addDataDisplay(championchipList);

		editor = ListEditor.of(new EditorSource<ChampionshipView>() {

			@Override
			public ChampionshipView create(int index) {
				return new ChampionshipView(activity.getUserList());
			}

		});

		selectionModel = new SingleSelectionModel<ChampionshipProxy>();
		selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {

			@Override
			public void onSelectionChange(SelectionChangeEvent event) {
				championshipPanel.clear();
				championshipPanel.add(editor.getEditors().get(provider.getList().indexOf(selectionModel.getSelectedObject())));
			}
		});
		championchipList.setSelectionModel(selectionModel);

		initWidget(uiBinder.createAndBindUi(this));
	}

	public void setPresenter(ChampionshipsActivity activity) {
		this.activity = activity;
	}

	static class ChampionshipCell extends AbstractCell<ChampionshipProxy> {

		@Override
		public void render(com.google.gwt.cell.client.Cell.Context context, ChampionshipProxy value, SafeHtmlBuilder sb) {
			sb.append(SafeHtmlUtils.fromString(value.getName()));
		}

	}

	@Override
	public ListEditor<ChampionshipProxy, ChampionshipView> asEditor() {
		return editor;
	}

	public void initProvider() {
		provider.setList(editor.getList());
	}

	public void selectFirstChampionship() {
		if (!provider.getList().isEmpty()) {
			selectionModel.setSelected(provider.getList().get(0), true);
		}
	}

	public void removeChampionshipEditor(ChampionshipProxy selectedProxy) {
		editor.getList().remove(selectedProxy);
		provider.refresh();
	}

}
